package comu.hnevc.zh.testdemo;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

import comu.hnevc.zh.testdemo.base.BaseUrl;
import comu.hnevc.zh.testdemo.dialog.NetSettingDialog;

public class LoginActivity extends AppCompatActivity {
    private  Button register;
    private Button netset;
    private  Button login;
    private EditText userName;
    private EditText password;
    private String result;
    private CheckBox remember_pwd;
    private  CheckBox auto_login;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private RequestQueue mQueue;

    @SuppressLint("WrongConstant")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
        editor=sharedPreferences.edit();
        register=findViewById(R.id.bt_register);
        netset=findViewById(R.id.bt_net_setting);
        login=findViewById(R.id.bt_login);
        userName=findViewById(R.id.et_username);
        password=findViewById(R.id.et_password);
        remember_pwd=findViewById(R.id.cb_remmber_password);
        auto_login=findViewById(R.id.cb_automatic_login);
        if (sharedPreferences.getBoolean("autologin",false)){
            Toast.makeText(this,"已选中自动登陆，页面将在3秒后跳转",Toast.LENGTH_SHORT).show();
            auto_login.setChecked(true);
            remember_pwd.setChecked(true);
            userName.setText(sharedPreferences.getString("UserName",""));
            password.setText(sharedPreferences.getString("UserPwd",""));
            Timer timer=new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);

                }
            },3000);
        }
        if (sharedPreferences.getBoolean("rememberpwd",false)){
            remember_pwd.setChecked(true);
            userName.setText(sharedPreferences.getString("UserName",""));
            password.setText(sharedPreferences.getString("UserPwd",""));

        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doLogin();
            }
        });
        netset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NetSettingDialog dialog=new NetSettingDialog(LoginActivity.this);
                dialog.setTitle("服务器IP地址");
                dialog.show();
            }
        });

    }

    private void doLogin() {
        final String user = userName.getText().toString();
        final String pwd = password.getText().toString();
        mQueue = new Volley().newRequestQueue(getApplicationContext());
        final JSONObject object = new JSONObject();
        try {
            object.put("UserName", user);
            object.put("User", pwd);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jor = new JsonObjectRequest(Request.Method.POST, BaseUrl.BASEURL + BaseUrl.user_login, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                result = jsonObject.optString("RESULT");
                Log.e("登陆", result + "\n" + user + "\n" + pwd);
                if (result.equals("S")) {
                    editor.putString("Username", user)
                            .putString("UserPwd", pwd)
                            .putString("UserRole", jsonObject.optString("UserRole"))
                            .commit();
                    rememberpwd();
                    autologin();
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();
                }else if (result.equals("F")){
                    Toast.makeText(LoginActivity.this,jsonObject.optString("ERRMSG"),Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(LoginActivity.this,"登陆错误",Toast.LENGTH_SHORT).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this,"网络错误",Toast.LENGTH_SHORT).show();

            }
        });
        mQueue.add(jor);
    }
        private void autologin() {
        if (auto_login.isChecked()){
            editor.putBoolean("autologin",true).commit();
        }else {
         editor.putBoolean("autologin",false).commit();
        }
        }

        private void rememberpwd() {
        if (remember_pwd.isChecked()){
            editor.putBoolean("remmberpwd",true).commit();
        }else {
            editor.putBoolean("rememberpwd",false).commit();
        }
        }
    }



